<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="物品名称" prop="goodsName">
              <el-input v-model="queryParams.goodsName" placeholder="请输入物品名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['demo:demo:export']" type="warning" plain icon="Download" @click="handleExport"> 导出 </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="inventoryList">
        <el-table-column label="序号" type="index" width="55" align="center" />
        <el-table-column label="主键" align="center" prop="id" />
        <el-table-column label="仓库名称" prop="warehouseName" />
        <el-table-column label="客户名称" prop="customerName" />
        <el-table-column label="物品名称" prop="goodsName" />
        <el-table-column label="板数" prop="stockCodeCount" />
        <el-table-column label="箱体名称" prop="goodsBoxName" />
        <el-table-column label="物品规格" prop="goodsStandardTypeName" />
        <el-table-column label="物品条数" prop="goodsBarTypeName" />
        <el-table-column label="物品重量" prop="goodsWeightName" />
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="InventorySummary" lang="ts">
import { listInventorySummary } from '@/api/wms/inventory';
import { InventoryVO, InventoryForm, InventoryQuery } from '@/api/wms/inventory/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const inventoryList = ref<InventoryVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const initFormData: InventoryForm = {
  id: undefined
};

const data = reactive<PageData<InventoryForm, InventoryQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    customerName: null,
    goodsName: null
  },
  rules: undefined
});

const { queryParams } = toRefs(data);

/** 查询测试单列表 */
const getList = async () => {
  loading.value = true;
  const res = await listInventorySummary(queryParams.value);
  inventoryList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'wms/inventory/summary/export',
    {
      ...queryParams.value
    },
    `demo_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
